package com.iweipan.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.iweipan.R;
import com.iweipan.base.WPBaseActivity;
import com.iweipan.bean.MsgListBean;
import com.iweipan.utils.sputils.SPUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

import static com.iweipan.base.WPUrlBase.WP_LOGIN_MSG;

/**
 * Created by xyb on 2016/12/26.
 */

public class WPMessageActivity extends WPBaseActivity implements AdapterView.OnItemClickListener, View.OnClickListener {

    WPMsgAdapter wpMsgAdapter;
    @BindView(R.id.iv_section_title_back)
    ImageView ivSectionTitleBack;
    @BindView(R.id.ll_section_title_back)
    LinearLayout llSectionTitleBack;
    @BindView(R.id.tv_section_title_title)
    TextView tvSectionTitleTitle;
    @BindView(R.id.tv_section_title_right)
    TextView tvSectionTitleRight;
    @BindView(R.id.ll_section_title_right)
    LinearLayout llSectionTitleRight;
    @BindView(R.id.rl_section_title)
    RelativeLayout rlSectionTitle;
    @BindView(R.id.msg_list)
    ListView msgList;

    private Intent custom_intent;

    @Override
    public int bindLayout() {
        return R.layout.activity_message;
    }

    @Override
    public void initToolbarColor() {

    }

    @Override
    public void initView(View view) {
        wpMsgAdapter = new WPMsgAdapter();
    }

    @Override
    public void doBusiness(Context mContext) {

        tvSectionTitleTitle.setText("消息中心");

        msgList.setAdapter(wpMsgAdapter);
        msgList.setOnItemClickListener(this);
        ivSectionTitleBack.setOnClickListener(this);

        String userId = (String) SPUtils.get(this, "userId", "userId");
        if (!userId.equals("userId")) {
            OkHttpUtils.get()
                    .addParams("userId", userId)
                    .url(WP_LOGIN_MSG)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            e.printStackTrace();
                        }

                        @Override
                        public void onResponse(String response, int id) {
//                            Log.e("WPMessageActivity", response);
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                int ret = jsonObject.getInt("ret");
                                if (ret == 0) {
                                    Gson gson = new Gson();
                                    MsgListBean msgListBean = gson.fromJson(response, MsgListBean.class);
                                    List<MsgListBean.NotificationMessageListBean> notificationMessageList = msgListBean.getNotificationMessageList();
                                    wpMsgAdapter.addList(notificationMessageList);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
        }

    }

    @Override
    public void resume() {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MsgListBean.NotificationMessageListBean item = (MsgListBean.NotificationMessageListBean) wpMsgAdapter.getItem(position);
        if (item.getActionType() == 1) {
            custom_intent = new Intent(this, ReceiverWebActivity.class);
            custom_intent.putExtra("webUrl", item.getActionUrl());
        } else if (item.getActionType() == 2) {
            custom_intent = new Intent(this, WPHomeActivity.class);
            custom_intent.putExtra("go_to", "two");
        }
        if (custom_intent != null) {
            startActivity(custom_intent);
        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_section_title_back:
                if (WPMessageActivity.this != null) {
                    finish();
                }
                break;

        }
    }


    private class WPMsgAdapter extends BaseAdapter {
        private List<MsgListBean.NotificationMessageListBean> notificationMessageList = new ArrayList<>();


        @Override
        public int getCount() {
            return notificationMessageList.size();
        }

        @Override
        public Object getItem(int position) {
            return notificationMessageList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public void addList(List<MsgListBean.NotificationMessageListBean> notificationMessageList) {
            this.notificationMessageList = notificationMessageList;
            notifyDataSetChanged();
        }

        @SuppressLint("ViewHolder")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (viewHolder == null) {
                convertView = View.inflate(WPMessageActivity.this, R.layout.adapter_message, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            MsgListBean.NotificationMessageListBean item = (MsgListBean.NotificationMessageListBean) getItem(position);
            String content = item.getContent();
            String title = item.getTitle();
            String createTime = item.getCreateTime();
            viewHolder.messageTime.setText(createTime);
            viewHolder.messageContent.setText(content);
            viewHolder.messageTitle.setText(title);

            return convertView;
        }


    }

    static class ViewHolder {
        @BindView(R.id.icon_message)
        ImageView iconMessage;
        @BindView(R.id.message_title)
        TextView messageTitle;
        @BindView(R.id.message_content)
        TextView messageContent;
        @BindView(R.id.message_item)
        LinearLayout messageItem;
        @BindView(R.id.message_time)
        TextView messageTime;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


}
